package com.yc.project.controller;

import com.yc.project.common.BaseResponse;
import com.yc.project.common.DeleteRequest;
import com.yc.project.common.ErrorCode;
import com.yc.project.common.ResultUtils;
import com.yc.project.exception.BusinessException;
import com.yc.project.model.dto.PostUpdateRequest;
import com.yc.project.service.PostService;
import com.yc.project.service.UserService;
import com.yc.yuapicommon.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 帖子接口
 *
 * @author yupi
 */
@RestController
@RequestMapping("/post")
@Slf4j
public class PostController {

    @Resource
    private PostService postService;

    @Resource
    private UserService userService;

    // region 增删改查



    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在

        boolean b = postService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新
     *
     * @param postUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updatePost(@RequestBody PostUpdateRequest postUpdateRequest,
                                            HttpServletRequest request) {
        if (postUpdateRequest == null || postUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        com.yc.project.model.entity.Post post = new com.yc.project.model.entity.Post();
        BeanUtils.copyProperties(postUpdateRequest, post);
        // 参数校验
        User user = userService.getLoginUser(request);
        long id = postUpdateRequest.getId();

        return ResultUtils.success(null);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<com.yc.project.model.entity.Post> getPostById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(null);
    }



    /**
     * 分页获取列表
     *
     * @param postQueryRequest
     * @param request
     * @return
     */


    // endregion

}
